package com.gotop.mapper.core.log.event;

import com.gotop.common.config.server.ServerInfo;
import com.gotop.common.constant.EventConstant;
import com.gotop.entity.log.LogApi;
import com.gotop.mapper.common.service.LogService;
import com.gotop.mapper.common.service.impl.LogServiceImpl;
import com.gotop.mapper.core.log.util.LogAbstractUtil;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.core.annotation.Order;
import org.springframework.scheduling.annotation.Async;

import java.util.Map;


/**
 * 异步监听日志事件
 *
 * @author superfish
 */
@Slf4j
@AllArgsConstructor
public class ApiLogListener {

    private final LogServiceImpl logService;
    private ServerInfo serverInfo;

    @Async
    @Order
    @EventListener(ApiLogEvent.class)
    public void saveApiLog(ApiLogEvent event) {
        Map<String, Object> source = (Map<String, Object>) event.getSource();
        LogApi logApi = (LogApi) source.get(EventConstant.EVENT_LOG);
        LogAbstractUtil.addOtherInfoToLog(logApi, serverInfo);
        logService.saveApiLog(logApi);
    }

}
